package com.test.demos.service.impl;


import com.test.demos.mapper.UserRoleMapper;
import com.test.demos.pojo.entity.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.test.demos.service.UserRoleService;

import java.util.List;

import static org.springframework.util.ObjectUtils.isEmpty;

/**
 * (UserRole)表服务接口
 *
 * @author twt
 * @since 2025-04-16 17:25:34
 */
@Service
@Transactional
public class UserRoleServiceImpl implements UserRoleService{

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param ruId 主键
     * @return 实例对象
     */
      @Override
    public UserRole queryById(Integer ruId) {
        UserRole select = userRoleMapper.selectOneById(ruId);
        if (isEmpty(select)) {
            throw new RuntimeException("未查询到对应数据");
        }
        return select;
    }

   /**
 * 插入数据
 * @param userRole
 */
  @Override
public void add(UserRole userRole) {
    Integer pkValue = userRole.getRuId();
        
        if(userRoleMapper.selectOneById(pkValue)!=null){
            throw new RuntimeException("数据已存在，无法插入");
        }
    int insert = userRoleMapper.insert(userRole);
    if (insert != 1) {
        throw new RuntimeException("插入数据失败");
    }
}

    @Override
    public List<UserRole> queryAll() {
        return userRoleMapper.selectAll();
    }

    /**
     * 更新数据
     * @param userRole
     */
      @Override
    public void update(UserRole userRole) {
        int i = userRoleMapper.update(userRole);
        if (i != 1) {
            throw new RuntimeException("更新数据失败");
        }
    }

    /**
     * 根据主键删除数据
     * @param ruId
     */
      @Override
    public void deleteById(Integer ruId) {
        int i = userRoleMapper.deleteById(ruId);
        if (i != 1) {
            throw new RuntimeException("删除数据失败");
        }
    }

}
